package com.cyf.db.mapper.smartCity;

import com.cyf.db.bean.InOutParking;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 出入记录
 * Created by Administrator on 2017/5/31 0031.
 */
@Mapper
public interface LocalInOutParkingMapper {

    @Select("SELECT * FROM local_inoutparking WHERE carLicense = #{carLicense} AND parkingId=#{parkingId} order by primaryOutTime desc")
    List<InOutParking> getByPlateAndParkingId(@Param("carLicense") String carLicense, @Param("parkingId") Integer parkingId);

    @Select("SELECT * FROM local_inoutparking WHERE carLicense = #{carLicense} order by primaryOutTime desc")
    List<InOutParking> getByPlate(@Param("carLicense") String carLicense);

    @Select("SELECT * FROM local_inoutparking WHERE id = #{id}")
    InOutParking getById(@Param("id") Integer id);

    @Select("SELECT localInoutparkingId,parkingId FROM local_inoutparking WHERE id = #{id}")
    InOutParking getLocalIdAndParkingIdById(@Param("id") Integer id);

    @Select("SELECT localInoutparkingId,parkingId, carLicense,recordOrderId FROM local_inoutparking WHERE id = #{id}")
    InOutParking getLocalIdAndParkingIdAndPlateById(@Param("id") Integer id);

    @Select("select count(id) from local_inoutparking where parkingId = #{parkingId} and primaryInTime >= #{start} and primaryInTime < #{end} ")
    Integer countForCarInByParkingIdAndTime(@Param("parkingId") Integer parkingId, @Param("start") String start, @Param("end") String end);

    @Select("select count(id) from local_inoutparking " +
            "where parkingId = #{parkingId} and primaryOutTime >= #{start} and primaryOutTime < #{end} ")
    Integer countForCarOutByParkingIdAndTime(@Param("parkingId") Integer parkingId, @Param("start") String start, @Param("end") String end);

    @Select("<script>" +
            "select * from local_inoutparking " +
            "<where>" +
            "   <if test=\" parkingId != 0 \">" +
            "      and parkingId = #{parkingId}" +
            "   </if>" +
            "   <if test=\" parkingIds != null \">" +
            "      and parkingId in " +
            "       <foreach collection='parkingIds' item='item' open='(' separator=',' close=')'> " +
            "           ${item} " +
            "       </foreach>" +
            "   </if>" +
            "   <if test=\" plate != null and plate != '' \">" +
            "      and carLicense like '${plate}%' " +
            "   </if>" +
            "   <if test=\" startTime != null and startTime != '' \">" +
            "      <![CDATA[ and primaryOutTime >= '${startTime} 00:00:00' ]]> " +
            "   </if>" +
            "   <if test=\" endTime != null and endTime != '' \">" +
            "      <![CDATA[ and primaryOutTime <= '${endTime} 23:59:59' ]]> " +
            "   </if>" +
            "</where>" +
            "order by primaryOutTime desc " +
            "limit #{start}, #{pageSize} " +
            "</script>")
    List<InOutParking> listByCondition(@Param("parkingId") int parkingId,
                                       @Param("parkingIds") List<Integer> parkingIds,
                                       @Param("startTime") String startTime,
                                       @Param("endTime") String endTime,
                                       @Param("plate") String plate,
                                       @Param("start") int start,
                                       @Param("pageSize") int pageSize);

    @Select("select * from local_inoutparking where parkingId=#{parkingId} and carLicense=#{plate} order by primaryOutTime desc limit 0,1")
    InOutParking selectInparkingByParkingIdAndPlate(@Param("parkingId") Integer parkingId, @Param("plate") String plate);
}